package com.example.taco_cloud.taco.controller;

import com.example.taco_cloud.taco.beans.Order;
import com.example.taco_cloud.taco.conf.OrderProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Slf4j
@Controller
@RequestMapping("/orders")
@ConfigurationProperties("taco.orders")
public class OrderController {

    @Autowired
    OrderProperty orderProperty;

    private int pageSize = 20;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @GetMapping("/current")
    public String orderForm(Model model) {
        log.info("pageSize = " + this.pageSize);
        log.info("orderProperty.pageSize = " + orderProperty.getPageSize());
        model.addAttribute("order", new Order());
        return "orderForm";
    }

    @PostMapping("processOrder")
    public String processOrder(Order order) {
        log.info("Order submitted: " + order);
        return "redirect:/";
    }

//    @GetMapping
//    public String ordersForUser(
//            @AuthenticationPrincipal User user, Model model) {
//        model.addAttribute("orders",
//                orderRepo.findByUserOrderByPlaceAtDesc(user));
//
//        return "orderList";
//    }
}
